package com.xmx.controller;

import com.xmx.entity.CrmUsers;
import com.xmx.entity.Roles;
import com.xmx.service.ICrmUsersService;
import com.xmx.service.IRolesService;
import com.xmx.service.IUser_rolesServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.provider.MD5;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * overview：
 * author:
 * 2023年05月04日 21:30:
 */
@Controller
public class CrmUserController {
    @Autowired
    private ICrmUsersService iCrmUsersService;
    @Autowired
    private IRolesService iRolesService;
    @Autowired
    private IUser_rolesServices iUser_rolesServices;

    @RequestMapping("queryUseraAndRoles")
    public String queryUseraAndRoles(Map map){
        //查询所有用户
        List<CrmUsers> crmUsers = iCrmUsersService.queryAllUsers();
        System.out.println("得到的用户集合："+crmUsers);
        for (CrmUsers crmUser : crmUsers) {
            Integer userid = crmUser.getUserid();
            //根据用户id，中间表和角色表联查
            List<Roles> rolesList = iRolesService.queryRolesByuserid(userid);
            System.out.println("联查得到的角色集合："+rolesList);
            crmUser.setRoles(rolesList);//将角色集合保存进用户
        }
        //查询所有角色信息
        List<Roles> rolesList = iRolesService.queryAllRoles();

        map.put("rolesList",rolesList);
        map.put("crmUsers",crmUsers);
        System.out.println("得到的用户777对象："+crmUsers);
        return "Manager/userManage";
    }
    //添加用户和角色信息
    @RequestMapping("/addUserAndRole")
    public String addUserAndRole(@RequestParam Map map){
        System.out.println("从页面获取的map:"+map);
        Integer uid = 0;//用户表id
        Integer rid = 0;//角色id
        String name = (String)map.get("username");
        String realname = (String)map.get("rname");
        if(realname !=null && !"".equals(realname) && name!= null && !"".equals(name)){
            CrmUsers crmUsers = new CrmUsers(null,name,name,realname);
            iCrmUsersService.addNewUser(crmUsers);
            uid=crmUsers.getUserid();
        }

        List<Roles> rolesList = iRolesService.queryAllRoles();
        System.out.println("角色循环："+rolesList);
        for (Roles roles : rolesList) {
            System.out.println("单个循环对象："+roles);
            //拼接页面name值
            String str = "rdesc"+roles.getRid();
            //得到选择的选项值
            String rname = (String)map.get(str);
            //判断是否有选项值
            if(rname !=null && !"".equals(rname)){
                Roles roles1 = iRolesService.queryRoleByname(rname);
                rid = roles1.getRid();
                System.out.println("用户uid："+uid+"角色rid:"+rid);
                iUser_rolesServices.addUser_role(uid,rid);//将多对多关系保存
            }
        }
        return "redirect:queryUseraAndRoles";
    }

    @RequestMapping("/delteByuserId")
    @ResponseBody
    public String delteByuserId(Integer id){
        iCrmUsersService.delteuserByid(id);
        iUser_rolesServices.deleteuru2(id);
        return "1";
    }

    @RequestMapping("/queryCrmUserById")
    public String queryCrmUserById(Integer id,Map map){
        System.out.println("整数id："+id);
        CrmUsers crmUsers = iCrmUsersService.queryCrmUserById(id);
        map.put("crmUsers",crmUsers);
        //查询所有角色
        List<Roles> allRoles = iRolesService.queryAllRoles();
        map.put("allRoles",allRoles);
        //根据userid查询对应角色
        List<Roles> rolesList = iRolesService.queryRolesByuserid(id);
        map.put("rolesList",rolesList);
        System.out.println("得到的橘色："+rolesList);
        return "Manager/userUpdate";
    }
    @RequestMapping("/updateUser")
    public String updateUser(CrmUsers crmUsers,@RequestParam Map map2,Map map){
        Integer uid = crmUsers.getUserid();
        Integer rid = 0;
        iCrmUsersService.updateUser(crmUsers);
        iUser_rolesServices.deleteuru2(uid);
        List<Roles> rolesList = iRolesService.queryAllRoles();
        System.out.println("角色循环："+rolesList);
        for (Roles roles : rolesList) {
            System.out.println("单个循环对象："+roles);
            //拼接页面name值
            String str = "rdesc"+roles.getRid();
            //得到选择的选项值
            String rname = (String)map2.get(str);
            //判断是否有选项值
            if(rname !=null && !"".equals(rname)){
                Roles roles1 = iRolesService.queryRoleByname(rname);
                rid = roles1.getRid();
                System.out.println("用户uid："+uid+"角色rid:"+rid);
                iUser_rolesServices.addUser_role(uid,rid);//将多对多关系保存
            }
        }
        return "redirect:addUserAndRole";
    }
}
